package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.system.domain.WbItemVisitLogs;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.WbProductCategoryBo;
import org.dromara.system.domain.vo.WbProductCategoryVo;
import org.dromara.system.domain.WbProductCategory;
import org.dromara.system.mapper.WbProductCategoryMapper;
import org.dromara.system.service.IWbProductCategoryService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 产品类别Service业务层处理
 *
 * @author Lion Li
 * @date 2024-03-22
 */
@RequiredArgsConstructor
@Service
public class WbProductCategoryServiceImpl implements IWbProductCategoryService {

    private final WbProductCategoryMapper baseMapper;

    /**
     * 查询产品类别
     */
    @Override
    public WbProductCategoryVo queryById(Long categoryId){
        return baseMapper.selectVoById(categoryId);
    }

    /**
     * 查询产品类别列表
     */
    @Override
    public TableDataInfo<WbProductCategoryVo> queryPageList(WbProductCategoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WbProductCategory> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(WbProductCategory::getCategoryId);
        Page<WbProductCategoryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询产品类别列表
     */
    @Override
    public List<WbProductCategoryVo> queryList(WbProductCategoryBo bo) {
        LambdaQueryWrapper<WbProductCategory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WbProductCategory> buildQueryWrapper(WbProductCategoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WbProductCategory> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getCategoryName()), WbProductCategory::getCategoryName, bo.getCategoryName());
        lqw.eq(bo.getParentCategoryId() != null, WbProductCategory::getParentCategoryId, bo.getParentCategoryId());
        return lqw;
    }

    /**
     * 新增产品类别
     */
    @Override
    public Boolean insertByBo(WbProductCategoryBo bo) {
        WbProductCategory add = MapstructUtils.convert(bo, WbProductCategory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCategoryId(add.getCategoryId());
        }
        return flag;
    }

    /**
     * 修改产品类别
     */
    @Override
    public Boolean updateByBo(WbProductCategoryBo bo) {
        WbProductCategory update = MapstructUtils.convert(bo, WbProductCategory.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WbProductCategory entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除产品类别
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
